<?php

namespace App\Http\Controllers\XhApi;


use App\Repository\XhApi\AdRepository;
use App\Repository\XhApi\GridRepository;
use App\Repository\XhApi\NavtabRepository;
use App\Repository\XhApi\SwiperRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class IndexController extends BaseController
{
    public $repoist;

    public function index(Request $request)
    {
        $cacheFile = '_xhPageIndex';
        // $data = $this->useCache ? Cache::get($cacheFile) : null;
        if (empty($data)) {
            $RepoistSwiper = new SwiperRepository();
            $where = [];
            $where['eq']['status'] = 0;
            $where['eq']['position'] = 0;
            $fields = ['pic', 'list_type', 'goods_id', 'rule_type'];
            $orderBy = ['sort' => 'asc'];
            $swiper = $RepoistSwiper->unifiedGetAll($where, $fields, $orderBy);

            $where = [];
            $where['eq']['status'] = 0;
            $RepoistGrid = new GridRepository();
            $fields = ['name', 'pic', 'list_type', 'goods_id', 'rule_type'];
            $grids = $RepoistGrid->unifiedGetAll($where, $fields, $orderBy);

            $where = [];
            $where['eq']['status'] = 0;
            $where['eq']['position'] = 0;
            $RepoistAd = new AdRepository();
            $fields = ['pic', 'list_type', 'goods_id', 'rule_type'];
            $orderBy = ['id' => 'desc'];
            $ads = $RepoistAd->unifiedGetAll($where, $fields, $orderBy);
            $where = [
                'position' => 1,
                'status' => 0
            ];
            $pop = $RepoistAd->unifiedGetOne($where, $fields);

            $where = [];
            $where['eq']['status'] = 0;
            $RepoistNavtab = new NavtabRepository();
            $orderBy = ['sort' => 'asc'];
            $fields = ['name', 'label', 'list_type', 'goods_ids', 'rule_type'];
            $tabs = $RepoistNavtab->unifiedGetAll($where, $fields, $orderBy);
            $data = [
                "swiper" => $swiper,
                "grid" => $grids,
                "ad" => $ads,
                "pop" => $pop,
                "tab" => $tabs
            ];
//            缓存半小时数据
            Cache::put($cacheFile, $data, 1800);
        }
        return $this->returnMsg(200, '数据已获取', $data);
    }
}
